package com.scsj.supermarket.adapter;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.scsj.supermarket.R;
import com.scsj.supermarket.bean.FiltrateBean;
import com.scsj.supermarket.customview.GridSpacingItemDecoration;
import com.vondear.rxtools.RxImageTool;

import java.util.ArrayList;
import java.util.List;

/**
 * 右侧滑动栏价格筛选
 */

public class FiltrateAdapter extends RecyclerView.Adapter<FiltrateAdapter.MyViewHolder> {

    private LayoutInflater minflater;
    private Context mContext;
    protected List<FiltrateBean> mDatas = new ArrayList<>();
    public FiltrateAdapter(Context context) {
        this.mContext = context;
        minflater = LayoutInflater.from(context);
    }
    public void addAllData(List<FiltrateBean> mDatas) {
        this.mDatas.addAll(mDatas);
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = minflater.inflate(R.layout.item_filtrate_layout, viewGroup, false);
        MyViewHolder viewHolder = new MyViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder myViewHolder, final int i) {
        FiltrateBean itemBean = mDatas.get(i);
        myViewHolder.parent_name_tv.setText(itemBean.getName());
        FiltrateChildAdapter filtrateChildAdapter = new FiltrateChildAdapter(mContext);
        myViewHolder.recycler_view.setAdapter(filtrateChildAdapter);
        filtrateChildAdapter.addAllData(itemBean.getFiltrateChildBeans());
    }

    @Override
    public int getItemCount() {
        if (mDatas != null) {
            return mDatas.size();
        } else {
            return 0;
        }
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        TextView parent_name_tv;
        RecyclerView recycler_view;
        public MyViewHolder(View itemView) {
            super(itemView);
            parent_name_tv = itemView.findViewById(R.id.parent_name_tv);
            recycler_view = itemView.findViewById(R.id.recycler_view);
            recycler_view.setLayoutManager(new GridLayoutManager(mContext, 3));
            recycler_view.setNestedScrollingEnabled(false);
            recycler_view.addItemDecoration(new GridSpacingItemDecoration(3, RxImageTool.dp2px(10), false));
        }
    }
}
